/*
* ============================================================================
*  Name        : main.cpp
*  Part of     : AI tools examples
*  Description :
*  Version     : %version: %
*  
*
* ============================================================================
*/
#include "sea_config.h"
#include "sea_defines.h"

#include "main.h"
#include "main.hxx"


#include "example_001.h"
#include "example_002.h"
#include "example_003.h"
#include "example_004.h"
#include "example_005.h"
#include "example_006.h"
#include "example_007.h"
#include "example_008.h"
#include "example_009.h"

#include "example_101.h"

/**
 * @file main.cpp
 *
 * \defgroup AssociationExamples EXAMPLES: Object Associations
 *
 * \defgroup StateMachineExamples EXAMPLES: State Machine Framework
 *
 */

using namespace sea;
using namespace sea_examples;

typedef void ( *SeaExample )( );
SeaExample examples[ ] = 
{
    launch_example_101,
    launch_example_102,

    launch_example_001,
    launch_example_002,
    launch_example_003,
    launch_example_0031,
    launch_example_0032,
    launch_example_0033,
    launch_example_004,
    launch_example_0041,
    launch_example_005,
    launch_example_0051,
    launch_example_006,
    launch_example_0061,
    launch_example_007,
    launch_example_008,
#if !defined( SEA_OS_SOS )
    launch_example_009,
#endif
};

#if defined( SEA_OS_LINUX )

#include <termios.h>
#include <unistd.h>
#include <fcntl.h>

int sea_examples::_kbhit( )
{
    struct termios oldt, newt;
    int ch;
    int oldf;

    tcgetattr( STDIN_FILENO, &oldt );
    newt = oldt;
    newt.c_lflag &= ~( ICANON | ECHO );
    tcsetattr( STDIN_FILENO, TCSANOW, &newt );
    oldf = fcntl( STDIN_FILENO, F_GETFL, 0 );
    fcntl( STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK );

    ch = getchar( );

    tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
    fcntl( STDIN_FILENO, F_SETFL, oldf );

    if( ch != EOF )
    {
        ungetc( ch, stdin );
        return 1;
    }
    return 0;
}

int sea_examples::_getch( )
{
    return getchar( );
}

#elif defined( SEA_OS_SOS )

int _kbhit( )
{
    return 0;
}

int _getch( )
{
    return 0;
}
#endif


int main( int argc, char* argv[ ] )
{
#if defined( SEA_OS_QT )
    QApplication app( argc,argv );
#else
    SEA_UNUSED( argc );
    SEA_UNUSED( argv );
#endif

    // Run examples
    int n = sizeof( examples ) / sizeof( SeaExample );
    for( int j = 0; j < n; j++ )
    {
        SEA_TRACE( "========================== \n" );
        examples[ j ]( );
    }

    return 0;
}

ACT_IMPL( Input, get_a_key )
{
    SEA_UNUSED_PARAM( );
    if( _kbhit( ) )
    {
        int key = _getch( );
        if( key )
        {
            push_event( Ev_Int_Key( ), key );
        }
    }
}

ACT_IMPL( Input, send_to_listeners )
{
    SEA_UNUSED_PARAM( );
    if ( m_listener )
    {
        m_listener->push_event( m_event_id, ACT_EVENT_PARAM( ) );
    }
}


